<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: zzh
  Date: 2022/4/19
  Time: 9:02 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户登录</title>
    <link type="text/css" rel="stylesheet" href="<c:url value="/static/css/github.css"/>">
</head>
<body>
<div>
    <center>
        <div>
            <div><h2>${title}</h2></div>
            <div>
                <div>
                    <div><h3>登录</h3></div>
                    <table>
                        <tr>
                            <th>账号:</th>
                            <th><input type="text" value="" id="username"></th>
                        </tr>
                        <tr>
                            <th>密码:</th>
                            <th><input type="password" value="" id="password"></th>
                        </tr>
                    </table>
                </div>
                <br>
                <div>
                    <button onclick="login()">登录</button>
                </div>
            </div>
        </div>
    </center>
</div>
</body>
<script type="text/javascript" src='<c:url value="/static/js/jquery.js" />'></script>
<script type="text/javascript">
    function login() {
        let username = document.getElementById("username").value;
        let password = document.getElementById("password").value;
        if (username.length > 0 && password.length > 0) {
            var settings = {
                "url": "${url}" + "/api/user/login",
                "method": "POST",
                "timeout": 0,
                "headers": {
                    "Content-Type": "application/json"
                },
                "data": JSON.stringify({
                    "stuNumber": username,
                    "password": password
                }),
            };

            $.ajax(settings).done(function (response) {
                if (response.status === 200) {
                    sessionStorage.setItem("user", JSON.stringify(response.data.user.tUser))
                    sessionStorage.setItem("token", response.data.user.token);
                    window.location.href = "${url}" + "/option?token=" + response.data.user.token
                } else {
                    alert(response.data.error);
                }
            });
        } else {
            alert("请输入账号或密码！")
        }
    }
</script>
</html>
